﻿using System.Configuration;
using System.Web;
using ElectronicCommerce.Framework.Configuration.Attributes;
using ElectronicCommerce.Framework.Configuration.ConfigSources.DotNet;
using ElectronicCommerce.Framework.Configuration.Utility;
using ElectronicCommerce.Framework.Diagnostics;
using ElectronicCommerce.Framework.Resources;

namespace ElectronicCommerce.Framework.Configuration.FrameworkConfig
{
    /// <summary>
    /// 表示 Framework 的配置。
    /// </summary>
    public class FrameworkConfig
    {
        public const string CONFIG_PATH = "~/Framework.config";

        /// <summary>
        /// Common Settings
        /// </summary>
        public static CommonSettings.CommonSettings CommonSettings
        {
            get
            {
                CommonSettings.CommonSettings commonSettings = GetConfig<CommonSettings.CommonSettings>();
                return commonSettings ?? Configuration.FrameworkConfig.CommonSettings.CommonSettings.Default;
            }
        }

        /// <summary>
        /// Config Settings
        /// </summary>
        public static ConfigSettings.ConfigSettings ConfigSettings
        {
            get
            {
                ConfigSettings.ConfigSettings commonSettings = GetConfig<ConfigSettings.ConfigSettings>();
                return commonSettings ?? Configuration.FrameworkConfig.ConfigSettings.ConfigSettings.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetConfig<T>() where T : ConfigurationSection, new()
        {
            DotNetConfigFileAttribute attribute = AttributeHelper.GetConfigAttribute<T, DotNetConfigFileAttribute>();
            if (attribute == null)
            {
                ConfigThrowHelper.ThrowConfigException(
                    R.ConfigError_NoConfigAttribute, typeof(T).FullName, typeof(DotNetConfigFileAttribute).FullName);
            }

            return GetConfig<T>(attribute.SectionName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public static T GetConfig<T>(string sectionName) where T : ConfigurationSection, new()
        {
            Check.Argument.IsNotEmpty("sectionName", sectionName);

            DotNetConfigGetParameter getParameter = new DotNetConfigGetParameter(CONFIG_PATH, sectionName);
            return ConfigSource.DotNet.GetConfig<T>(getParameter, true);
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool RestartAppDomain()
        {
            bool restarted = false;
            try
            {
                HttpRuntime.UnloadAppDomain();
                restarted = true;
            }
            catch
            {
                restarted = false;
            }

            return restarted;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFrameworkConfigPath(string fileName)
        {
            return ConfigSettings.GetConfigPath(false, string.Format("{0}/{1}", "Framework", fileName));
        }
    }
}
